{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f8fe8a5a-8e28-404c-812a-c11250e87792",
   "metadata": {},
   "source": [
    "# type\n",
    "查询变量所指的对象类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "138ec063-e981-44b6-a414-dc9264141037",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number = 1\n",
    "type(number)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d9587af-d904-4a15-8f1f-e51f6d53995c",
   "metadata": {},
   "source": [
    "# isinstance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "28b4d00d-3442-4753-8df6-855104d00557",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number = 1\n",
    "isinstance(1, int)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd304dcb-3e20-49c6-a397-44b3f5762cb4",
   "metadata": {},
   "source": [
    "# 两者的区别\n",
    "* type() 不会认为子类是一种父类类型\n",
    "* isinstance() 会认为子类是一种父类类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cb87c02f-825b-4ef0-92bc-f45150fc9697",
   "metadata": {},
   "outputs": [],
   "source": [
    "class A:\n",
    "    pass\n",
    "\n",
    "class B(A):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6f4719ab-5ecc-4344-bf54-3f01149c1d81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(A(), A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8402c5c5-f9f9-4610-b4c6-ba81f0395142",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(A()) == A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2d3edcfc-85db-413a-ac19-604700fc879c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(B(), A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "64d64bf9-5a0c-4867-b4d2-5a2669cfa438",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(B()) == A"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
